package com.sunhao.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.audiofx.BassBoost;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;

import com.sunhao.myapplication.util.PermissionUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //短信读写权限
    private static final String[] PERMISSIONS = new String[]{
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_SMS,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS
    };


//    private static final String[] PERMISSIONS_CONTACTS = new String[]{
//
//    };

    private static final int REQUEST_CODE_ALL = 1;
    private static final int REQUEST_CODE_CONTACTS = 2;
    private static final int RRQUEST_CODE_SMS = 3;


    //不请求权限，就不会再调用该方法了
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull @org.jetbrains.annotations.NotNull String[] permissions, @NonNull @org.jetbrains.annotations.NotNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

//        requestCode是在前面定义时传入的

        //获取权限
        switch (requestCode) {
            case REQUEST_CODE_ALL:
                if (PermissionUtil.checkGrant(grantResults)) {
                    Toast.makeText(this, "所有权限获取成功", Toast.LENGTH_SHORT).show();
                } else {
                    //如何具体到哪个权限获取失败
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                            switch (permissions[i]) {
                                case Manifest.permission.READ_CONTACTS:
                                case Manifest.permission.WRITE_CONTACTS:
                                    //如果没有授权
                                    Toast.makeText(this, "联系人授权失败", Toast.LENGTH_SHORT).show();
                                    jumpToSettings();
                                    return;

                                case Manifest.permission.READ_SMS:
                                case Manifest.permission.SEND_SMS:
                                    //如果没有授权
                                    Toast.makeText(this, "短信授权失败", Toast.LENGTH_SHORT).show();
                                    jumpToSettings();
                                    return;
                            }
                        }
                    }
                }
                break;
            case REQUEST_CODE_CONTACTS:
                //如果授权了

                if (PermissionUtil.checkGrant(grantResults)) {
                    Toast.makeText(this, "联系人授权成功", Toast.LENGTH_SHORT).show();
                } else {
                    //如果没有授权
                    Toast.makeText(this, "联系人授权失败", Toast.LENGTH_SHORT).show();
                    jumpToSettings();
                }
                break;
            case RRQUEST_CODE_SMS:

                if (PermissionUtil.checkGrant(grantResults)) {
                    Toast.makeText(this, "短信授权成功", Toast.LENGTH_SHORT).show();
                } else {
                    //如果没有授权
                    Toast.makeText(this, "短信授权失败", Toast.LENGTH_SHORT).show();
                    jumpToSettings();
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_contact).setOnClickListener(this);
        findViewById(R.id.btn_message).setOnClickListener(this);
        PermissionUtil.checkPermissions(this, PERMISSIONS, REQUEST_CODE_ALL);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_contact:
                PermissionUtil.checkPermissions(this,new String[]{PERMISSIONS[0],PERMISSIONS[1]}, REQUEST_CODE_CONTACTS);
                break;

            case R.id.btn_message:
//                PermissionUtil.checkPermissions(this, PERMISSIONS_SMS, RRQUEST_CODE_SMS);
                PermissionUtil.checkPermissions(this,new String[]{PERMISSIONS[2],PERMISSIONS[3]}, REQUEST_CODE_CONTACTS);
                break;
        }
    }

    private void jumpToSettings() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", getPackageName(), null));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}